import axios from "axios"
import defaultConfig from "./default"

const BASEURL = process.env.NODE_ENV === "development"
                    ? defaultConfig.baseUrl.dev 
                    : defaultConfig.baseUrl.pro;

class HttpRequest {
    constructor(baseUrl) {
        this.baseURL = baseUrl
        this.queue = {}
    }
    getInsideConfig() {
        const CONFIG = {
            baseURL: this.baseURL,
            timeout: 10000,
            header: {}
        }
        return CONFIG;
    }
    // 请求劫持
    interceptor(instance, url) {
        instance.interceptors.request.use(config => {
            console.log("config ：：", config)
            return config;
        }, err => {
            return Promise.reject(err);
        })
        instance.interceptors.response.use(data => {
            console.log("data ：：", data)
            return data.data
        }, err => {
            return Promise.reject(err);
        })
    }
    // 请求
    request(opts) {
        opts = Object.assign(this.getInsideConfig(), opts)
        let axisoInstance = axios.create(opts)
        this.interceptor(axisoInstance, opts.url)
        return axisoInstance(opts);
    }
}
console.log(BASEURL)
const axiosObj = new HttpRequest(BASEURL)
export default axiosObj;
